<?php
/**
 * 评论相关函数 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: func_comment.php 378 2010-04-25 04:56:15Z lisijie86 $
*/

/**
 * 更新一条评论
 *
 * $updata数组的键名必须是comment表中的字段，如 $updata = array('author' => 'lisijie')
 * 
 * @param int $comment_id 评论ID
 * @param array $updata 更新的内容数组
 * @param int
 */
function update_comment($comment_id, $updata) {
	global $db;
	$comment_id = intval($comment_id);
	$updata = apply_filter('update_comment', $updata);
	if ( isset($updata['id']) ) unset($updata['id']);
	return $db->update('comment', $updata, array('id'=>$comment_id));
}

/**
 * 删除一条评论
 *
 * @param int $comment_id
 * @return void
 */
function delete_comment($comment_id) {
	global $db;
	do_action('delete_comment_before', $comment_id);
	$comment_id = intval($comment_id);
	$comment = $db->fetch_one_array("SELECT id,userid,postid,`status` FROM ".tname('comment')." WHERE id = '$comment_id'");
	if ( !$comment ) return FALSE;
	$db->query("DELETE FROM ".tname('comment')." WHERE id = '$comment_id'");
	if ( $comment['userid'] ) {
		$db->query("UPDATE ".tname('user')." SET comments = comments - 1 WHERE id = '$comment[userid]'");
	}
	if ( $comment['status'] == 1 ) {
		$db->query("UPDATE ".tname('post')." SET comments = comments - 1 WHERE id = '$comment[postid]'");
	}
	do_action('delete_comment_after', $comment_id);
}

/**
 * 获取一条评论
 *
 * @param int $comment_id 评论ID
 * @return array 评论
 */
function get_comment($comment_id) {
	global $db, $_CACHE;
	$comment = $db->fetch_one_array("SELECT * FROM ".tname('comment')." WHERE `status` = 1 AND id = '$comment_id'");
	if ( !$comment ) return FALSE;
	$_CACHE['current_comment'] = $comment;
	return $comment;
}

/**
 * 提取文章评论
 *
 * @param int $post_id 文章ID
 * @param int $num 提取数量
 * @param int $offset 偏移量
 * @return void
 */
function query_comment($post_id, $num=0, $offset=0) {
	global $_CACHE,$db;
	$order = config('reverse_order') ? 'DESC' : 'ASC';
	if ( $post_id ) {
		$wheresql = " AND postid = '$post_id'";
	}
	$sql = "SELECT * FROM ".tname('comment')." WHERE `status` = 1 $wheresql ORDER BY id {$order}";
	if ( $num ) {
		$sql .= " LIMIT $offset, $num";
	}
	$_CACHE['comment'] = array();
	$query = $db->query($sql);
	while ( $row = $db->fetch_array($query) ) {
		$_CACHE['comment'][$row['id']] = $row;
	}
	$db->free_result($query);
}

/**
 * 是否有评论
 *
 * @return bool
 */
function have_comment() {
	global $_CACHE;
	return !empty($_CACHE['comment']);
}

/**
 * 提取一条评论，并将数组中的内部指针向前移动一位
 * 
 * @return mixed 返回提取的评论，如没有数据或到达末端，则返回False
 */
function fetch_comment() {
	global $_CACHE;
	if ( $_CACHE['comment'] ) {
		$_CACHE['current_comment'] = current($_CACHE['comment']);
		next($_CACHE['comment']);
		return $_CACHE['current_comment'];
	}
	return false;
}

/**
 * 提取当前评论的字段
 *
 * @param string $field 字段名
 * @param bool $return True返回，False直接输出，默认False
 * @return mixed
 */
function comment($field, $return = false) {
	global $_CACHE;
	if ( $_CACHE['current_comment'] ) {
		switch ( $field ) {
			case 'posttime' :
				$value = get_date($_CACHE['current_comment']['dateline']); break;
			case 'author_link' : 
				$value = $_CACHE['current_comment']['author'];
				if ( $_CACHE['current_comment']['url'] ) {
					$value = '<a href="'.$_CACHE['current_comment']['url'].'" rel="external nofollow" target="_blank">'.$value.'</a>';
				}
				break;
			case 'avatar' :
				$value = avatar($_CACHE['current_comment']['email'], 32, 'G', false); break;
			default:
				$value = $_CACHE['current_comment'][$field]; break;
		}
		$value = apply_filter("comment_{$field}", $value);
		if ( $return ) {
			return $value;
		}
		echo $value;
	}
	return false;
}
?>